/*
 * Copyright (C) 2013 Fabiano Tarlao
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.easybenchmark.client.benchmarks;

import org.easybenchmark.client.arbiter.Result;
import org.easybenchmark.client.compatibilityutils.JRE;

/**
 *
 * @author Fabiano
 */
public abstract class Benchmark {
    protected  long executionTime;
    protected  Double averageExecutionTime;
    protected static int WARMUP=11000; 
    /**
     * This field may contain a numeric value which is related to the weight of the computational effort
     * this is  an extimation, and is not comparable between different tests. It is useful in order to find out implementation
     * issues.
     */
    protected long workDone=0;
    private String benchmarkDescrition="default description, proper value may be set in subclass constructor";
    
    public Benchmark() {
    }
    
    protected int lastShotNumberIteration;
    
    public long start(int iterations){
        this.lastShotNumberIteration=iterations;
        initWarmUpTask();
        for(int j = 0;j< WARMUP;j++){
            this.workDone+=benchTask();
            //MainEntryPoint.applicationRoot.logLabel.setText("workDone "+workDone);
        }
       initBenchTask();
       workDone=0;
        long startTime = System.currentTimeMillis();
        for(int j = 0;j< iterations;j++){
            this.workDone+=benchTask();
            //MainEntryPoint.applicationRoot.logLabel.setText("workDone "+workDone);
        }
        long endTime = System.currentTimeMillis();
        executionTime= endTime-startTime;
        averageExecutionTime = new Double(executionTime)/iterations;
        return(endTime-startTime);
    }
    
    /**
     * One time intialization of  the benchmark enviroment, this is executed once and its execution time is not computed.
     * @return it depends, may be a workload
     */
    protected abstract int initWarmUpTask();
    
    
    /**
     * One time intialization of  the benchmark enviroment, this is executed once and its execution time is not computed.
     * @return it depends, may be a workload
     */
    protected abstract int initBenchTask();
    /**
     * This is the benchmarked task 
     * @return this value shoud be a workload  measure.. and should be the execution outcome in order to avoid aggresive optimizations
     */
    protected abstract long benchTask();

    public String getBenchmarkDescrition() {
        return benchmarkDescrition;
    }   
    
    public String getBenchmarkName(){
        return(this.getClass().getName());
    }

    protected final void setBenchmarkDescrition(String benchmarkDescrition) {
        this.benchmarkDescrition = benchmarkDescrition;
    }
    
    public Double getAverageExecutionTime() {
        return averageExecutionTime;
    }

    public long getExecutionTime() {
        return executionTime;
    }

    public long getWorkDone() {
        return workDone;
    }
    
    public long getWorkDonePerIteration(){
        return (lastShotNumberIteration!=0)? (workDone/lastShotNumberIteration) : 0;
    }
    
    public Result getResult(){
        Result result = new Result();
        result.averageExecutionTime=getAverageExecutionTime();
        result.benchmarkDescription= getBenchmarkDescrition();
        result.benchmarkName=JRE.getSimpleClassName(this.getClass());
        result.executionTime=getExecutionTime();
        result.iterations=lastShotNumberIteration;
        result.workDone=getWorkDone();
        result.workDonePerIteration=getWorkDonePerIteration();
        return result;
    }

    
}
